package xyz.perkd.util;

import org.apache.fontbox.ttf.TTFParser;
import org.apache.fontbox.ttf.TrueTypeCollection;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.fontbox.util.autodetect.FontFileFinder;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.util.Matrix;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;

public class WaterMaker {



    private static final String DEFAULT_TTF_FILENAME = "simsun.ttf";
    private static final String DEFAULT_TTC_FILENAME = "simsun.ttc";
    private static final String DEFAULT_FONT_NAME = "SimSun";
    private static final TrueTypeFont DEFAULT_FONT;

    static {
        DEFAULT_FONT = loadSystemFont();
    }


    /**
     * 加载系统字体,提供默认字体
     *
     * @return
     */
    private synchronized static TrueTypeFont loadSystemFont() {
        //load 操作系统的默认字体. 宋体
        FontFileFinder fontFileFinder = new FontFileFinder();
        for (URI uri : fontFileFinder.find()) {
            try {
                final String filePath = uri.getPath();
                if (filePath.endsWith(DEFAULT_TTF_FILENAME)) {
                    return new TTFParser(false).parse(new RandomAccessReadBufferedFile(new File(filePath)));
                } else if (filePath.endsWith(DEFAULT_TTC_FILENAME)) {
                    TrueTypeCollection trueTypeCollection = new TrueTypeCollection(new FileInputStream(filePath));
                    final TrueTypeFont font = trueTypeCollection.getFontByName(DEFAULT_FONT_NAME);
                    //复制完之后关闭ttc
                    trueTypeCollection.close();
                    return font;
                }
            } catch (Exception e) {
                throw new RuntimeException("加载操作系统字体失败", e);
            }
        }

        return null;
    }

    public static void addWaterMark(int idx, PDDocument document, PDPage page, String ts) {
        try {
            PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
            PDFont font = PDType0Font.load(document, DEFAULT_FONT, true);;
            float fontSize = 30;
            PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
            // 水印透明度
            r0.setNonStrokingAlphaConstant(0.2f);
            r0.setAlphaSourceFlag(true);
            cs.setGraphicsStateParameters(r0);
            cs.setNonStrokingColor(0.8F, 0, 0);
            cs.beginText();
//            String ts = "能力有限公司";
            cs.setFont(font, fontSize);
            float h = ts.length() * fontSize;
//            for (int i = 0; i <= 10; i++) {
                // 获取旋转实例
//                cs.setTextMatrix(Matrix.getRotateInstance(-150, 100, 0));
                cs.showText(ts);
//                for (int j = 0; j < 20; j++) {
//                    cs.setTextMatrix(Matrix.getRotateInstance(-150, i * 100, j * h));
//                    cs.showText(ts);
//                }
//            }
            cs.endText();
            cs.restoreGraphicsState();
            cs.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
